/*
** convert_base.c for my_printf in /home/nadali_t//local/svn/tek2ed/my_printf
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Sun Nov 18 16:32:57 2012 thomas nadalie
** Last update Sun Nov 18 18:03:32 2012 thomas nadalie
*/

#include	<stdlib.h>
#include	"my.h"

char		*convert_func(int nb, char *base, int i, int count2)
{
  int		count;
  char		*str;

  if (nb < 0)
    nb = 1;
  while (base[i] != '\0')
    {
      i = i + 1;
    }
  count = i;
  while ((nb / i) > 0)
    {
      i = i * count;
    }
  str = malloc(sizeof(*str) + count);
  while (i > count)
    {
      i = i / count;
      str[count2] = base[(nb / i) % count];
      count2 = count2 + 1;
    }
  str[count2] = (base[nb % count]);
  str[count2 + 1] = '\0';
  return (str);
}

char		*convert_base(char *nbr, char *base_from, char *base_to)
{
  int		i;
  int		j;
  int		nb;
  int		base;

  i = 0;
  j = 0;
  nb = 0;
  base = 0;
  if (nbr[0] == '-')
    nbr[0] = '0';
  while (base_from[base] != '\0')
    base = base + 1;
  while (nbr[j] != '\0')
    {
      while (nbr[j] != base_from[i])
	i = i + 1;
      nb = nb * base + i;
      j = j + 1;
      i = 0;
    }
  return (convert_func(nb, base_to, 0, 0));
}
